package dormouse.vfs.ui;

/**
 * <p>Title: Ammonite</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2001</p>
 * <p>Company: Kymerix</p>
 * @author Mark Fortner
 * @version 1.0
 */
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;

public class FileNodeRenderer extends DefaultTreeCellRenderer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5862500282716325978L;
	protected JCheckBox checkBox = new JCheckBox("backup");
	private Component strut = Box.createHorizontalStrut(5);
	private JPanel panel = new JPanel();

	public FileNodeRenderer() {
		panel.setBackground(UIManager.getColor("Tree.textBackground"));

		setOpaque(false);
		checkBox.setOpaque(false);
		panel.setOpaque(false);

		panel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		panel.add(this);
		panel.add(strut);
		panel.add(checkBox);

	}
	public Component getTreeCellRendererComponent(
						JTree tree, Object value,
						boolean selected, boolean expanded,
						boolean leaf, int row,
						boolean hasFocus) {
		FileNode node = (FileNode)value;

		super.getTreeCellRendererComponent(
								tree, value, selected, expanded,
								leaf, row, hasFocus);

		checkBox.setVisible(node.isDirectory());
		checkBox.setSelected(node.isSelected());

		return panel;
	}
	public Dimension getCheckBoxOffset() {
		Graphics g = panel.getGraphics();
		int xoffset = 0;

		if(g != null) {
			try {
				FontMetrics fm = g.getFontMetrics();
				xoffset = fm.stringWidth(getText()) +
						  strut.getPreferredSize().width;
			}
			finally  {
				g.dispose();
			}
		}
		return new Dimension(xoffset, 0);
	}
}